package com.hisan.freeride.home.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.hisan.freeride.R;
import com.hisan.freeride.common.base.AppConfig;
import com.hisan.freeride.common.base.BaseActivity;
import com.hisan.freeride.common.base.BaseAppLication;
import com.hisan.freeride.common.callback.DialogCallback;
import com.hisan.freeride.common.utils.CollectionUtils;
import com.hisan.freeride.common.utils.utils.MyHandlerUtil;
import com.hisan.freeride.databinding.CommentBinding;
import com.hisan.freeride.home.adapter.CommentAdapter;
import com.hisan.freeride.home.model.WelfareModel;
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.lzy.okgo.OkGo;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建时间 : 2017/7/8
 * 创建人：yangyingqi
 * 公司：嘉善和盛网络有限公司
 * 备注：评价页面
 */
public class CommentActivity extends BaseActivity<CommentBinding> {
    private CommentAdapter welfareAdapter;
    private int page = 1;
    private List<WelfareModel> welfareModels=new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return  R.layout.comment;
    }

    @Override
    protected void initView () {
        welfareAdapter = new CommentAdapter(new ArrayList<>(0), this);
        mBinding.rv.setAdapter(welfareAdapter);
        mBinding.rv.setLayoutManager(new LinearLayoutManager(this));
        mBinding.setStateModel(mStateModel);
    }

    @Override
    protected void initEvent() {
        mBinding.topBack.setOnClickListener(v -> finishActivity(true));
        welfareAdapter.setButtonClickListener((type, data) -> {
            if (!CollectionUtils.isNullOrEmpty(data)) {
                if (type==0){
                    getzan(data.getId(),type);
                }else if (type == 1) {
                    getzan(data.getId(),type);
                } else if (type == 2) {
                    Bundle bundle = new Bundle();
                    bundle.putInt("driver_id", data.getDriver_id());
                    startActivityForResult(LoveCartDetailsActivity.class, 0, bundle,true);
                }else if (type==3){
                    Bundle bundle = new Bundle();
                    bundle.putInt("driver_id", data.getDriver_id());
                    startActivityForResult(LoveCartDetailsActivity.class, 0, bundle,true);
                }
            }
        });
        welfareAdapter.setOnItemClickListener(data -> {
            if (!CollectionUtils.isNullOrEmpty(data)) {
                int id=data.getId();
                Bundle budle=new Bundle();
                budle.putInt("id",id);
                budle.putSerializable("data",data);
                startActivityForResult(WelfareMessageActivity.class,0,budle,true);
            }
        });
        mBinding.pull.setRefreshListener(new BaseRefreshListener() {
            @Override
            public void refresh() {
                page=1;
                welfareModels.clear();
                loadData(true);
                mBinding.pull.finishRefresh();

            }

            @Override
            public void loadMore() {
                page++;
                loadData(true);
                mBinding.pull.finishLoadMore();
            }
        });
    }

    @Override
    protected void loadData(boolean isRefresh) {
        OkGo.<List<WelfareModel>>get(AppConfig.UCList)
                .params("page",page+"")
                .params("size",20)
                .execute(new DialogCallback<List<WelfareModel>>(this) {
                    @Override
                    public void onSuccess(com.lzy.okgo.model.Response<List<WelfareModel>> response) {
                        super.onSuccess(response);
                        if (!CollectionUtils.isNullOrEmpty(response.body())){
                            showData();
                            if (isOk(response)){
                                if(response.body().size()>=20){
                                    mBinding.pull.setCanLoadMore(true);
                                }else {
                                    mBinding.pull.setCanLoadMore(false);
                                }
                                welfareModels.addAll(response.body());
                                welfareAdapter.setNewData(welfareModels);
                            }else {
                                showErrorView(response.code());
                            }
                        }else {
                            if (!CollectionUtils.isNullOrEmpty(welfareModels))welfareAdapter.setNewData(welfareModels);
                            else noData();
                        }
                    }
                    @Override
                    public void onError(com.lzy.okgo.model.Response<List<WelfareModel>> response) {
                        super.onError(response);
                        showErrorView(response.code());
                    }
                });
    }
    //点赞
    public void getzan(int id,int type) {
        OkGo.post(AppConfig.Getzan)
                .params("id",id)
                .execute(new DialogCallback<Object>(this) {
                    @Override
                    public void onSuccess(com.lzy.okgo.model.Response<Object> response) {
                        super.onSuccess(response);
                        if (!CollectionUtils.isNullOrEmpty(response)){
                            welfareModels.clear();
                            page=1;
                            if (type==0){
                                showToast("取消成功");
                            }else {
                                showToast("点赞成功");
                            }

                            MyHandlerUtil.getHandler().postDelayed(() -> loadData(true),200);
                        }
                    }

                    @Override
                    public void onError(com.lzy.okgo.model.Response<Object> response) {
                        super.onError(response);
                        showError(response);
                    }
                });

    }
}
